package com.ynet.middleground.mobile.approve.utils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ynet.middleground.approve.bean.*;

/**
 * BusinessDataGenerator 生成移动审批业务数据工具
 *
 * @author suremotoo
 * @date 2023/03/23 10:12
 */
public class BusinessDataGenerator {

    static final String MODULE_NAME_PREFIX = "模块_";

    static final String GROUP_NAME_PREFIX = "分组_";

    static final String TAG_NAME_PREFIX = "标签_";

    static final String CHILD_DATA_TITLE_PREFIX = "子数据大标题_";

    static final Map<String, List<String>> TABLE_DATA = new HashMap<>();

    static {
        TABLE_DATA.put("channelName", Arrays.asList("小贷", "保理", "易融"));
        TABLE_DATA.put("reason", Arrays.asList("欠费", "过期", "造假"));
    }

    public static void main(String[] args) {
        BusinessDataGenerator generator = new BusinessDataGenerator();

        //// 仅一层
        // List<BusinessModuleData> businessData = generator.generateModuleData(2, 2, 2, 4, null);
        //// System.out.println("JSON.toJSONString(businessData) = "
        //// + JSON.toJSONString(businessData, SerializerFeature.DisableCircularReferenceDetect));
        //
        //// 2 层
        // List<BusinessModuleData> businessDataWithChildData = generator.generateModuleData(2, 2, 2, 4, businessData);
        //// System.out.println("JSON.toJSONString(businessDataWithChildData) = "
        //// + JSON.toJSONString(businessDataWithChildData, SerializerFeature.DisableCircularReferenceDetect));
        //
        //// 3 层
        // List<BusinessModuleData> businessDataWithChildData3 =
        // generator.generateModuleData(2, 2, 2, 4, businessDataWithChildData);
        //
        int level = 1;
        String businessData = generator.generateBusinessData(level);
        System.out.println("businessData = " + businessData);

        exportFile(level + "_level_businessData.json", businessData);
    }

    public String generateBusinessData(int level) {
        List<BusinessModuleData> init = generateModuleData(2, 2, 2, 4, null);
        for (int i = 0; i < level; i++) {
            if (i == 0) {
                continue;
            }
            init = generateModuleData(2, 2, 2, 4, init);
        }
        return JSON.toJSONString(init, SerializerFeature.DisableCircularReferenceDetect);
    }

    private List<BusinessModuleData> generateModuleData(Integer moduleNums, Integer groupNums, Integer tagNums,
        int tableRows, List<BusinessModuleData> childData) {
        List<BusinessModuleData> moduleDataList = new ArrayList<>();
        for (int i = 0; i < moduleNums; i++) {
            BusinessModuleData data = new BusinessModuleData();
            data.setGroupName(MODULE_NAME_PREFIX + i + generateRandomNumber());
            data.setIsHiddenName(false);
            data.setData(generateGroupData(groupNums, tagNums, tableRows, childData));
            data.setOrder(0);
            data.setIsHidden(false);
            moduleDataList.add(data);
        }
        return moduleDataList;
    }

    private List<BusinessGroupData> generateGroupData(Integer groupNums, Integer tagNums, int tableRows,
        List<BusinessModuleData> childData) {
        BusinessChildData businessChildData = new BusinessChildData();
        businessChildData.setTitle(CHILD_DATA_TITLE_PREFIX + "from_group" + generateRandomNumber());
        businessChildData.setIsHiddenTitle(false);
        businessChildData.setIsBtnActive(false);
        businessChildData.setBtnName("buttonName");
        businessChildData.setData(childData);
        List<BusinessGroupData> groupDataList = new ArrayList<>();
        for (int i = 0; i < groupNums; i++) {
            BusinessGroupData data = new BusinessGroupData();
            data.setSubGroupName(GROUP_NAME_PREFIX + generateRandomNumber());
            data.setIsHiddenName(false);
            data.setIsCollapse(false);
            data.setData(generateTagData(tagNums, tableRows, childData));
            // TODO: 子数据
            // data.setChildData((i == 0 && childData != null) ? businessChildData : null);
            data.setOrder(0);
            data.setIsHidden(false);
            groupDataList.add(data);
        }

        return groupDataList;
    }

    private List<BusinessTagData> generateTagData(Integer tagNums, int tableRows, List<BusinessModuleData> childData) {
        BusinessChildData businessChildData = new BusinessChildData();
        businessChildData.setTitle(CHILD_DATA_TITLE_PREFIX + "from_tag" + generateRandomNumber());
        businessChildData.setIsHiddenTitle(false);
        businessChildData.setIsBtnActive(false);
        businessChildData.setBtnName("buttonName");
        businessChildData.setData(childData);
        List<BusinessTagData> tagDataList = new ArrayList<>();
        for (int i = 0; i < tagNums; i++) {
            BusinessTagData data = new BusinessTagData();
            data.setTagName(TAG_NAME_PREFIX + i + generateRandomNumber());
            data.setIsHiddenName(true);
            data.setData(generateFormData(childData));
            // TODO: 子数据
            // data.setChildData((i == 0 && childData != null) ? businessChildData : null);
            data.setOrder(0);
            data.setIsHidden(false);
            tagDataList.add(data);
        }

        BusinessTagData dataFile = new BusinessTagData();
        dataFile.setTagName(TAG_NAME_PREFIX + "files" + generateRandomNumber());
        dataFile.setIsHiddenName(false);
        dataFile.setData(generateFormFilesData());
        dataFile.setChildData(null);
        dataFile.setOrder(0);
        dataFile.setIsHidden(false);
        tagDataList.add(dataFile);

        List<BusinessFormData> table = new ArrayList<>();
        table.add(generateFormTablesData(tableRows, childData));
        BusinessTagData dataTable = new BusinessTagData();
        dataTable.setTagName(TAG_NAME_PREFIX + "table" + generateRandomNumber());
        dataTable.setIsHiddenName(false);
        dataTable.setData(table);
        dataTable.setChildData(null);
        dataTable.setOrder(0);
        dataTable.setIsHidden(false);
        tagDataList.add(dataTable);
        return tagDataList;
    }

    private List<BusinessFormData> generateFormData(List<BusinessModuleData> childData) {
        List<BusinessFormData> formDataList = new ArrayList<>();
        BusinessFormData textForm = new BusinessFormData();
        textForm.setIdName("text-form-1");
        textForm.setDisplayName("姓名" + generateRandomNumber());
        textForm.setType("text");
        textForm.setOrder(0);
        textForm.setIsHidden(false);
        textForm.setIsReadonly(false);
        textForm.setValue("测试公司");
        // textForm.setChildData(new BusinessChildData());
        formDataList.add(textForm);

        BusinessChildData businessChildData = new BusinessChildData();
        businessChildData.setTitle(CHILD_DATA_TITLE_PREFIX + "from_form" + generateRandomNumber());
        businessChildData.setIsHiddenTitle(false);
        businessChildData.setIsBtnActive(false);
        businessChildData.setBtnName("buttonName");
        businessChildData.setData(childData);

        BusinessFormData textForm2 = new BusinessFormData();
        textForm2.setIdName("text-form-2");
        textForm2.setDisplayName("手机号" + generateRandomNumber());
        textForm2.setType("text");
        textForm2.setOrder(0);
        textForm2.setIsHidden(false);
        textForm2.setIsReadonly(false);
        textForm2.setValue("13344556677");
        textForm.setChildData(childData != null ? businessChildData : null);
        formDataList.add(textForm2);

        BusinessFormData imgForm = new BusinessFormData();
        imgForm.setIdName("img-form-1");
        imgForm.setDisplayName("小图" + generateRandomNumber());
        imgForm.setType("img");
        imgForm.setOrder(0);
        imgForm.setIsHidden(false);
        imgForm.setIsReadonly(false);
        imgForm.setValue("zhongtai/test-weixiao.jpg");
        formDataList.add(imgForm);

        BusinessFormData imgForm2 = new BusinessFormData();
        imgForm2.setIdName("img-form-2");
        imgForm2.setDisplayName("大图" + generateRandomNumber());
        imgForm2.setType("img");
        imgForm2.setOrder(0);
        imgForm2.setIsHidden(false);
        imgForm2.setIsReadonly(false);
        imgForm2.setValue("zhongtai/test-screenshot.png");
        formDataList.add(imgForm2);

        return formDataList;
    }

    private List<BusinessFormData> generateFormFilesData() {
        List<BusinessFormData> formDataList = new ArrayList<>();
        BusinessFormData fileForm = new BusinessFormData();
        fileForm.setIdName("file-form-1");
        fileForm.setDisplayName(generateRandomNumber() + "附件1.pdf");
        fileForm.setType("file");
        fileForm.setOrder(0);
        fileForm.setIsHidden(false);
        fileForm.setIsReadonly(false);
        fileForm.setValue("zhongtai/1310878801743400963.pdf");
        formDataList.add(fileForm);

        BusinessFormData fileForm2 = new BusinessFormData();
        fileForm2.setIdName("file-form-2");
        fileForm2.setDisplayName(generateRandomNumber() + "附件-testHelpCenter.pdf");
        fileForm2.setType("file");
        fileForm2.setOrder(0);
        fileForm2.setIsHidden(false);
        fileForm2.setIsReadonly(false);
        fileForm2.setValue("zhongtai/testHelpCenter.pdf");
        formDataList.add(fileForm2);

        return formDataList;
    }

    private BusinessFormData generateFormTablesData(int tableRows, List<BusinessModuleData> childData) {
        BusinessFormData formData = new BusinessFormData();
        formData.setIdName("table-form");
        formData.setDisplayName("表格-拉黑渠道列表" + generateRandomNumber());
        formData.setType("table");
        formData.setOrder(0);
        formData.setIsHidden(false);
        formData.setIsReadonly(false);

        formData.setColumns(generateColumns());

        formData.setValue(JSON.toJSONString(generateTableValues(tableRows, childData),
            SerializerFeature.DisableCircularReferenceDetect));

        return formData;
    }

    private List<BusinessTableData> generateColumns() {
        List<BusinessTableData> tableData = new ArrayList<>();
        BusinessTableData column1 = new BusinessTableData();
        column1.setName("channelName");
        column1.setTitle("渠道名称");
        tableData.add(column1);

        BusinessTableData column2 = new BusinessTableData();
        column2.setName("reason");
        column2.setTitle("理由");
        tableData.add(column2);
        return tableData;
    }

    private List<Map<String, Object>> generateTableValues(int nums, List<BusinessModuleData> childData) {
        BusinessChildData businessChildData = new BusinessChildData();
        businessChildData.setTitle(CHILD_DATA_TITLE_PREFIX + "from_table" + generateRandomNumber());
        businessChildData.setIsHiddenTitle(false);
        businessChildData.setIsBtnActive(false);
        businessChildData.setBtnName("buttonName");
        businessChildData.setData(childData);

        List<Map<String, Object>> tableList = new ArrayList<>();
        boolean child = true;
        for (int i = 0; i < nums; i++) {
            Map<String, Object> cell = new HashMap<>();
            List<BusinessTableData> columns = generateColumns();
            for (BusinessTableData column : columns) {
                List<String> list = TABLE_DATA.get(column.getName());
                int index = new Random().nextInt(list.size());
                boolean finalChild = child;
                HashMap<String, Object> cellPro = new HashMap<String, Object>() {
                    {
                        put("value", list.get(index));
                        put("childData", (finalChild && childData != null) ? businessChildData : null);
                    }
                };
                child = false;
                cell.put(column.getName(), cellPro);
            }
            tableList.add(cell);
        }
        return tableList;
    }

    public static String generateRandomNumber() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 5; i++) {
            int digit = random.nextInt(10);
            sb.append(digit);
        }
        return sb.toString();
    }

    private static void exportFile(String resourceName, String content) {
        // 将图片生成到D盘的目录下
        String filePath = "/Users/suremotoo/Desktop/" + resourceName;
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath))) {
            bufferedWriter.write(content);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
